// pages/musicList/musicList.js
// import Pubsub from 'pubsub-js'
import pubsubSong from '../../utils/pubsubSong'
import axios from '../../utils/axios'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    musicHeadrerInfo: '',
    musicList: [],
    musicListTitle: '',
    musicDesc: '',
    // musicList的高度
    musicListHeight: 900,
    songindex:0
  },

  // 去往播放页面
  tosong(e) {
    const songindex = e.currentTarget.dataset.songindex

    // 进入song页面时记录下点击的歌曲的index,以便在musiclist找到对应的上一首或者下一首歌曲信息
    this.setData({
      songindex,
    })
    const songid = e.currentTarget.dataset.songid
    wx.navigateTo({
      url: '/pages/song/song?songid=' + songid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
 const res=  await  axios('/playlist/detail', { id: options.musiclistid,s: 0})
      this.setData({
        musicHeadrerInfo: res.playlist.coverImgUrl,
        musicListTitle: res.playlist.name,
        musicDesc: res.playlist.description,
        musicList: res.playlist.tracks.splice(0, 60).map(item => {
          return {
            id: item.id,
            name: item.name,
            songer: item.ar[0].name,
            songCover: item.alia[0]
          }
        }),
      });
  const singindex=  pubsubSong(this.data.songindex,this.data.musicList)
  this.setData(singindex)

    //  const res= await axios('/playlist/detail', { id: options.musiclistid,s: 0})
    //  console.log(res);
    //  this.setData({
    //   musicHeadrerInfo:res.playlist.coverImgUrl,
    //   musicList:res.playlist.tracks
    //  })
    //     console.log(this.data.musicList, this.data.musicHeadrerInfo);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 加载更多音乐
  loadMoreMusic() {
    if (this.data.musicListHeight > 3060) return

    this.setData({
      musicListHeight: this.data.musicListHeight + 900
    })
    console.log(this.data.musicListHeight);
  }
})